package com.baizhi.service.impl;

import java.util.List;
import java.util.Random;

import com.baizhi.dao.BookDao;
import com.baizhi.dao.CategoryDao;
import com.baizhi.entity.Page;
import com.baizhi.entity.Product;
import com.baizhi.service.BookService;
import com.baizhi.util.MyBatisUtil;

public class BookServiceImpl implements BookService {
	private BookDao bd = null;
	/*
	 * 查询热卖
	 */
	@Override
	public List<Product> findHotBooks() {
		bd = MyBatisUtil.getMapper(BookDao.class);
		return bd.selectHotBooks();
	}
	/*
	 * 编辑推荐图书
	 */
	@Override
	public List<Product> findRecommendBooks() {
		bd = MyBatisUtil.getMapper(BookDao.class);
		//获取商品总数量
		Integer count = bd.countBooks();
		//生成随机数
		Random random = new Random();
		int begin = random.nextInt(count-2)+1;
		return bd.selectRecommendBooks(begin, begin+1);
	}
	/*
	 *最新上架图书 
	 */
	@Override
	public List<Product> findNewBooks() {
		bd = MyBatisUtil.getMapper(BookDao.class);
		return bd.selectNewBooks();
	}
	/*
	 * 最新上架的热卖图书
	 */
	@Override
	public List<Product> findNewHotBooks() {
		bd = MyBatisUtil.getMapper(BookDao.class);
		return bd.selectNewHotBooks();
	}
	/*
	 *查询单个图书 
	 */
	@Override
	public Product findBookById(Integer id) {
		bd = MyBatisUtil.getMapper(BookDao.class);
		Product p = bd.selectBookById(id);
		CategoryDao cd = MyBatisUtil.getMapper(CategoryDao.class);
		p.setCategory(cd.getCategoryByPId(id));
		return p;
	}
	/*
	 * 根据分类ID查询商品
	 */
	@Override
	public List<Product> findBoooksByCid(Integer id,Integer iid, Page page,Integer flag) {
		bd = MyBatisUtil.getMapper(BookDao.class);
		page.setTotalRows(bd.getCountNum(id, iid,flag));
		return bd.selectBooksByCId(id,iid,page,flag);
	}
}
